
package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.CgtCustService;
import com.lt.test.api.KtTakeStockItemService;
import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.KtTakeStockItemDao;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
public class KtTakeStockItemServiceImpl implements KtTakeStockItemService {

	@Autowired
	private KtTakeStockItemDao ktTakeStockItemDao;
	
	@Autowired
	CgtCustService cgtCustService;
	
	public List query(Map parameter) {
		return ktTakeStockItemDao.query(parameter);
	}
	
	public int create(Map parameter) {
		return ktTakeStockItemDao.create(parameter);
	} 
	
	public int update(Map parameter) {
		return ktTakeStockItemDao.update(parameter);
	}
	
	public int save(Map parameter) {
		return ktTakeStockItemDao.save(parameter);
	}
	 
	public int delete(Long id) {
		return ktTakeStockItemDao.delete(id);
	}
    public Map findById(Long id) {
		return ktTakeStockItemDao.findById(id);
	} 
	public int batch(List values){
    	return ktTakeStockItemDao.batch(values);
    }
	public int batchUpdate(List values) {
		return ktTakeStockItemDao.batchUpdate(values);
	}
	public int batchDelete(List values) {
		return ktTakeStockItemDao.batchDelete(values);
	}

	@Override
	public int saveTakeSrockItem(Map parameter) {
		String barCode = parameter.get("barCode").toString();
		String shopId = parameter.get("shopId").toString();
		String takeId = parameter.get("takeId").toString();
		String takeNumber = parameter.get("takeNumber").toString();   //盘点数
		Map<String, Object> productMap = ktTakeStockItemDao.getProduct(barCode, shopId);
		if(productMap == null){
			productMap = new HashMap<>();
		}

		List list = query(parameter);
		if(list == null || list.size() <= 0) {
			productMap.put("id", StringUtils.getUUID());
			productMap.put("takeId", takeId);
			productMap.put("shopId", shopId);
			productMap.put("barCode", barCode);
			productMap.put("takeNumber", takeNumber);
			productMap.put("status", "1");
			productMap.put("operateTime", new Date());
			return ktTakeStockItemDao.create(productMap);
		}else {
			String id = ((Map) list.get(0)).get("id").toString();
			parameter.put("id", id);
			return ktTakeStockItemDao.update(parameter);
		}
	}

	@Override
	public List<Map<String, Object>> queryTakeSrockItem(Map parameter) {
		String takeId = parameter.get("takeId").toString();
		String beginTime = parameter.get("beginTime") == null ? "" : parameter.get("beginTime").toString();
		String endTime = parameter.get("endTime") == null ? "" : parameter.get("endTime").toString();
		return ktTakeStockItemDao.queryKtTakeStockItemList(takeId, beginTime, endTime);
	}

}
